package com.example.base.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.base.system.domain.SysConfig;
import com.example.base.system.domain.vo.SysConfigEnum;
import com.example.base.system.domain.vo.SysConfigVo;
import com.example.base.system.domain.vo.SystemConfigVo;
import com.example.base.system.service.SysConfigService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.base.config.PageData;
import com.example.base.excetion.GlobalExcetion;
import com.example.base.limit.IPRateLimit;
import com.example.base.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiong
 * @since 2024-02-07
 */
@RestController
@Api(tags = {"系统config的服务类"})
@RequestMapping("/sys-config")
public class SysConfigController {

        @Autowired
        private SysConfigService sysConfigService;

        @GetMapping("/getsystemconfig")
        public JSONObject getSystemConfig(){
                List<SysConfig> sysConfigList=sysConfigService.getSystemSysConfig();
                Map<String,Object> map=new HashMap<>();
                sysConfigList.forEach(e->{
                        map.put(e.getConfigKey(),
                                e.getConfigValue()==null ? "" : e.getConfigValue());
                });
                return Result.successInfo(map);
        }

        @GetMapping("/getmailconfig")
        public JSONObject getMailConfig(){
                List<SysConfig> sysConfigList=sysConfigService.getMailSysConfig();
                Map<String,Object> map=new HashMap<>();
                sysConfigList.forEach(e->{
                        map.put(e.getConfigKey(),
                                e.getConfigValue()==null ? "" : e.getConfigValue());
                });
                return Result.successInfo(map);
        }

        @GetMapping("/getextconfig")
        public JSONObject getExtConfig(){
                List<SysConfig> sysConfigList=sysConfigService.getExtSysConfig();
                return Result.successInfo(sysConfigList);
        }


        @PostMapping("/updatesystemconfig")
        public JSONObject updateSystemConfig(@RequestBody JSONObject jsonObject){
               if(jsonObject!=null){
                   jsonObject.forEach((k,v)->{
                        sysConfigService.updateSysConfig(SysConfigEnum.SYSTEM.getType(),k,String.valueOf(v));
                   });
               }
               return Result.successInfo("更新成功");
        }


        @PostMapping("/updatemailconfig")
        public JSONObject updateMailConfig(@RequestBody JSONObject jsonObject){
                if(jsonObject!=null){
                        jsonObject.forEach((k,v)->{
                                sysConfigService.updateSysConfig(SysConfigEnum.MAIL.getType(),k,String.valueOf(v));
                        });
                }
                return Result.successInfo("更新成功");
        }

        @PostMapping("saveorupdate")
        public JSONObject saveOrUpdateConfig(@RequestBody SysConfigVo vo) throws GlobalExcetion {
                sysConfigService.saveOrUpdateSysConfig(vo);
                return Result.successInfo("更新成功");
        }

        @PostMapping("/delete")
        public JSONObject deleteController(@RequestBody JSONObject jsonObject){
                if(!jsonObject.containsKey("id")){
                    return Result.errorInfo("id不得为空");
                }
                sysConfigService.removeById(String.valueOf(jsonObject.get("id")));
                return Result.successInfo("删除成功");
        }


        @PostMapping("/save")
        @IPRateLimit(name = "SysConfig-save",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的新增接口",notes = "的新增接口")
        public JSONObject save(@RequestBody SysConfig sysConfig) throws GlobalExcetion{
                 sysConfigService.saveSysConfig(sysConfig);
                 return Result.successInfo("保存成功");
        }


        @PostMapping("/update")
        @IPRateLimit(name = "SysConfig-update",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的更新接口",notes = "的更新接口")
        public JSONObject update(@RequestBody SysConfig sysConfig) throws GlobalExcetion{
                sysConfigService.updateSysConfigById(sysConfig);
                return Result.successInfo("更新成功");
        }



        @DeleteMapping("/{id}")
        @IPRateLimit(name = "SysConfig-del",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的删除接口",notes = "的删除接口")
        public JSONObject delete(@PathVariable("id") Serializable id) throws GlobalExcetion{
                boolean flag = sysConfigService.removeById(id);
                if(flag){
                        return Result.successInfo("删除成功");
                }else {
                        return Result.errorInfo("删除失败");
                }
        }

        @PostMapping("/del/batch")
        @IPRateLimit(name = "SysConfig-delbatch",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的批量删除接口",notes = "的批量删除接口")
        public JSONObject deleteBatch(@RequestBody List<Integer> ids) throws GlobalExcetion {
                boolean flag =  sysConfigService.removeByIds(ids);
                if(flag){
                        return Result.successInfo("删除成功");
                }else {
                        return Result.errorInfo("删除失败");
                }
        }

        @GetMapping("/getall")
        @IPRateLimit(name = "SysConfig-getall",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的获取全部接口",notes = "的获取全部接口")
        public JSONObject findAll() throws GlobalExcetion {
                List<SysConfig> list= sysConfigService.list();
                return Result.successInfo(list);
        }

        @GetMapping("/{id}")
        @IPRateLimit(name = "SysConfig-getid",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的获取一个接口",notes = "的获取一个接口")
        public JSONObject findOne(@PathVariable("id") Serializable id) throws GlobalExcetion {
                SysConfig data= sysConfigService.getById(id);
                return Result.successInfo(data);
        }

        @GetMapping("/page")
        @IPRateLimit(name = "SysConfig-page",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "的分页接口",notes = "的分页接口")
        public JSONObject findPage(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                        @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize) throws GlobalExcetion {
                QueryWrapper<SysConfig> queryWrapper = new QueryWrapper<>();
                IPage<SysConfig> data=sysConfigService.page(new Page<>(page, pageSize),queryWrapper);
                PageData pageData=new PageData(data.getTotal(),page,pageSize,data.getRecords());
                return Result.successInfo(pageData);
        }
}

